package com.lz.bit.operation;

import org.junit.Test;

/**
 * BitOperation..
 *
 * @author Lizhong
 * @date 2019/4/10
 */
public class BitOperation {
    /**
     * ExecutorService状态码
     */
    static final int COUNT_BITS = Integer.SIZE - 3;
    static final int CAPACITY = (1 << COUNT_BITS) - 1;

    final int RUNNING = -1 << COUNT_BITS;
    final int SHUTDOWN = 0 << COUNT_BITS;
    final int STOP = 1 << COUNT_BITS;
    final int TIDYING = 2 << COUNT_BITS;
    final int TERMINATED = 3 << COUNT_BITS;

    @Test
    public void test() {

        // mmp RUNNING=~CAPACITY
        System.out.println(RUNNING | 0);
        System.out.println("~Cap" + (~CAPACITY) + ";" + runStateOf(RUNNING));

        System.out.printf("\n%x\n%x\n%x\n%x\n%x\n", RUNNING, SHUTDOWN, STOP, TIDYING, TERMINATED);
     test(1 << COUNT_BITS);
     test(SHUTDOWN);
     test(STOP);

    }

    @Test
    public void test02() {
        int i = ctlOf(RUNNING, 12);
        int i1 = runStateOf(i);
        int i2 = workerCountOf(i);
        // 将running status与work 绑在一起
        System.out.printf("RUNNING:%d,i:%d,i1:%d,i2:%d", RUNNING, i, i1, i2);
        Thread.currentThread().getThreadGroup();
    }

    private static int runStateOf(int c) {
        return c & ~CAPACITY;
    }

    private static int workerCountOf(int c) {
        return c & CAPACITY;
    }

    private static int ctlOf(int rs, int wc) {
        return rs | wc;
    }



    public void test(int n) {
        String temp = "";
        if (n == 0) {
            System.out.println(0);
        } else if (n > 0) {
            temp = fuzhu(n);
            System.out.println(temp);
        } else {
            System.out.println("无法计算负数");
        }
    }

    public String fuzhu(int n) {
        String temp = "";
        while (n > 0) {
            if (n % 2 == 1) {
                temp = 1 + temp;
            } else {
                temp = 0 + temp;
            }
            n = n / 2;
        }
        return temp;
    }

}
